//
//  CheckBottomSectionView.m
//  AirQuality
//
//  Created by Po on 15/9/23.
//  Copyright © 2015年 Po. All rights reserved.
//

#import "CheckBottomSectionView.h"

@implementation CheckBottomSectionView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initInterface];
        self.eventStatus = YES;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeAirName:) name:@"airName" object:nil];
    }
    return self;
}

- (void)initInterface
{
    CGFloat width = ScreenWidth / 4 - 1;
    CGFloat height = self.frame.size.height;
    NSArray * titles = @[@"站点",@"",@"最大值",@"最小值"];
    for (NSUInteger i = 0; i < titles.count; i ++) {
        CGRect rect = CGRectMake((width + 1) * i, 0, width, height);
        if (i == 1) {
            _button = [[UIButton alloc] initWithFrame:rect];
            [_button setTitle:@"AQI" forState:UIControlStateNormal];
//            _button.titleLabel.font = [UIFont systemFontOfSize:[self textFond]];
//            _button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, width-62);
//            [_button setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
            _button.titleLabel.font = [UIFont systemFontOfSize:16];
//            _button.imageEdgeInsets = UIEdgeInsetsMake(0, width/2-[self textFond]+35, 0,0);
//            _button.imageEdgeInsets = UIEdgeInsetsMake(0,width-17, 0,4);
            [_button setBackgroundColor:SysMainColor];
            [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:_button];
            _button.userInteractionEnabled = NO;
            continue;
        }
        [self addSubview:[self createLabelWithFrame:rect title:titles[i]]];
    }
}

- (void)changeAirName:(NSNotification *)notification{
    
    [_button setTitle:notification.object forState:UIControlStateNormal];
   
}

- (UILabel *)createLabelWithFrame:(CGRect)rect title:(NSString *)title
{
    UILabel * label = [[UILabel alloc] initWithFrame:rect];
    [label setBackgroundColor:SysMainColor];
    [label setText:title];
//    label.font = [UIFont systemFontOfSize:[self textFond]];
    [label setTextColor:[UIColor whiteColor]];
    [label setTextAlignment:NSTextAlignmentCenter];
    return label;
}

- (void)pressButton:(UIButton *)sender
{
    self.eventStatus = !self.eventStatus;
//    if (self.eventStatus) {
//        [_button setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
//    }else{
//       [_button setImage:[UIImage imageNamed:@"箭头2"] forState:UIControlStateNormal];
//    }
    if (_delegate) {
        [_delegate checkBottomSection:self didSelect:0];
    }
}

//字体大小适配
- (CGFloat)textFond{
    CGFloat textFond;
    if (DeviceType >= 4 && DeviceType < 5) {
        textFond = 0;
    }else if (DeviceType >= 5 && DeviceType < 6){
        textFond = 2;
    }else{
        textFond = 8;
    }
    return textFond;
}
@end
